﻿using HideRibbonItems.Layouts.HideRibbonItems;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace HideRibbonItems.ControlTemplates.HideRibbonItems
{
    public partial class HideRibbonItems : UserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {            
            List<RibbonLibraryItem> rliList = getValidLibraries();
            // Om vi ska köra denna kontroll på biblioteket
            if (rliList.Count > 0)
            {            
                // Hämta ribbon   
                SPRibbon ribbon = SPRibbon.GetCurrent(this.Page);

                if (ribbon != null)
                {
                    foreach(RibbonLibraryItem rli in rliList)
                    {
                        // Dölj knappar
                        foreach (string ribbonItem in rli.RibbonItems)
                        {
                            bool addMenu = true;
                            if (rli.Groups.Count > 0)
                            {
                                // Get logged in user
                                SPUser user = SPContext.Current.Web.CurrentUser;
                                foreach (SPGroup group in user.Groups)
                                {
                                    // Finns användaren med i gruppen ska ej objektet döljas
                                    if (rli.Groups.Contains(group.Name))
                                    {
                                        addMenu = false;
                                        break;
                                    }

                                }
                            }
                            if (addMenu)
                            {
                                ribbon.TrimById("Ribbon." + ribbonItem);
                            }
                        }
                    }
                }
            }
        }

        private List<RibbonLibraryItem> getValidLibraries()
        {
            List<RibbonLibraryItem> rliList = new List<RibbonLibraryItem>();
            Layouts.HideRibbonItems.ribbonItems ri = new Layouts.HideRibbonItems.ribbonItems();
            List<RibbonLibraryItem> rliListAll = ri.getWebPropertyRibbonMenu();
            foreach (RibbonLibraryItem rliTemp in rliListAll)
            {
                foreach (string libraryname in rliTemp.Documents)
                {

                    if (SPContext.Current.List != null && SPContext.Current.List.Title.ToLower().Equals(libraryname.ToLower()))
                    {
                        rliList.Add(rliTemp);
                        
                    }
                }                
            }
            return rliList;               
        }
    }        
}
